
package com.insigma.business.qrcode.soldier.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;

import com.insigma.business.common.utils.PriInfoHandleUtil;
import com.insigma.business.qrcode.common.dao.SoldierBasicInfoBDao;
import com.insigma.business.qrcode.common.dto.SoldierApplyTypeEnum;
import com.insigma.business.qrcode.common.dto.search.SoldierBasicInfoBSearch;
import com.insigma.business.qrcode.common.entity.SoldierBasicInfoB;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.DateUtils;
import com.insigma.business.qrcode.common.vo.ProcessStatusEnum;
import com.insigma.business.qrcode.intell.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.dto.SoldierBasicInfoBSearcher;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.soldier.entity.SoldierApplyInfoDO;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.entity.SoldierOuterBasicInfoDO;
import com.insigma.business.qrcode.soldier.service.SoldierApplyInfoService;
import com.insigma.business.qrcode.soldier.service.SoldierOuterBasicInfoService;
import com.insigma.business.qrcode.soldier.util.PrivacyEncUtils;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.insigma.business.qrcode.soldier.dao.SoldierBasicInfoDao;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service("soldierBasicInfoService")
public class SoldierBasicInfoServiceImpl implements SoldierBasicInfoService {

    @Autowired(required = false)
    private SoldierBasicInfoDao soldierBasicInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Resource
    SoldierApplyInfoService soldierApplyInfoService;

    @Resource
    SoldierOuterBasicInfoService soldierOuterBasicInfoService;


    
    @Override
    public List<SoldierBasicInfoDO> search(final SoldierBasicInfoBSearcher soldierBasicInfoBSearcher) {


        if (Strings.isNotBlank(soldierBasicInfoBSearcher.getEmploymentFlag())) {
            Date date = org.apache.commons.lang3.time.DateUtils.addMonths(new Date(), -3);
            String startCalDateTime = DateUtil.formatDate(date, "yyyy-MM");
            soldierBasicInfoBSearcher.setStartCalDateTime(startCalDateTime);
        }

        if (Strings.isNotBlank(soldierBasicInfoBSearcher.getAdminAreaCode())) {
            soldierBasicInfoBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(soldierBasicInfoBSearcher.getAdminAreaCode()));
        }
        return soldierBasicInfoDao.search(soldierBasicInfoBSearcher);
    }

    @Override
    public Integer count(SoldierBasicInfoBSearcher innerSearcher) {
        return soldierBasicInfoDao.count(innerSearcher);
    }

    
    @Override
    public SoldierBasicInfoDO get(final String id) {
        return soldierBasicInfoDao.get(id);
    }

    
    @Override
    public Integer insert(final SoldierBasicInfoDO soldierBasicInfoDO) {
        soldierBasicInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.SOLDIER_BASIC_INFO_ID));
        return soldierBasicInfoDao.insert(soldierBasicInfoDO);
    }

    
    @Override
    public Integer update(final SoldierBasicInfoDO soldierBasicInfoDO) {
        return soldierBasicInfoDao.update(soldierBasicInfoDO);
    }

    @Override
    public Integer delete(String id, String modifierType, String modifierId) {
        return null;
    }


    @Override
    public SoldierBasicInfoDO getByIdCardNo(String idCardNo) {
        return decryptDo(soldierBasicInfoDao.getByIdCardNo(PriInfoHandleUtil.encrypt(idCardNo)));
    }

    private SoldierBasicInfoDO decryptDo(SoldierBasicInfoDO soldierBasicInfoDO) {
        if (ObjectUtil.isNotEmpty(soldierBasicInfoDO)) {
            soldierBasicInfoDO.setMobile(PriInfoHandleUtil.decrypt(soldierBasicInfoDO.getMobile()));
            soldierBasicInfoDO.setIdCardNo(PriInfoHandleUtil.decrypt(soldierBasicInfoDO.getIdCardNo()));
        }
        return soldierBasicInfoDO;
    }

    @Override
    public SoldierBasicInfoDO getBySoldierId(String soldierId) {
        return decryptDo(soldierBasicInfoDao.getBySoldierId(soldierId));
    }

    @Override
    public SoldierBasicInfoDO getByQrSuccess(String soldierId) {
        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        if (soldierApplyInfoDO == null) {
            
            List<SoldierApplyInfoDO> soldierApplyInfoDOS = soldierApplyInfoService.listBySoldierId(soldierId, SoldierApplyTypeEnum.OUTSIDER.name());
            if (!CollectionUtils.isEmpty(soldierApplyInfoDOS)) {
                if (soldierApplyInfoDOS.get(0).getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                    
                    SoldierOuterBasicInfoDO base = soldierOuterBasicInfoService.getBySoldierId(soldierId);
                    SoldierBasicInfoDO baseInfo = new SoldierBasicInfoDO();
                    BeanUtils.copyProperties(base, baseInfo);
                    return decryptDo(baseInfo);
                }
            }

            
            SoldierApplyInfoDO soldierApplyTemp = soldierApplyInfoService.getLastApplyOfSoldierId(soldierId, SoldierApplyTypeEnum.TEMPORARY.name());
            if (soldierApplyTemp != null) {
                if (!checkTemporaryQrIsExpired(soldierApplyTemp)) {
                    SoldierBasicInfoDO baseInfo = new SoldierBasicInfoDO();
                    BeanUtils.copyProperties(soldierApplyTemp, baseInfo);
                    return decryptDo(baseInfo);
                }
            }
        } else {
            if (soldierApplyInfoDO.getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                SoldierBasicInfoDO base = soldierBasicInfoDao.getBySoldierId(soldierId);
                return decryptDo(base);
            }
        }
        return null;
    }

    @Override
    public SoldierBasicInfoDO getByQrSuccessOfHzAndFhz(String soldierId) {
        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        if (soldierApplyInfoDO == null) {
            
            List<SoldierApplyInfoDO> soldierApplyInfoDOS = soldierApplyInfoService.listBySoldierId(soldierId, SoldierApplyTypeEnum.OUTSIDER.name());
            if (!CollectionUtils.isEmpty(soldierApplyInfoDOS)) {
                if (soldierApplyInfoDOS.get(0).getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                    
                    SoldierOuterBasicInfoDO base = soldierOuterBasicInfoService.getBySoldierId(soldierId);
                    SoldierBasicInfoDO baseInfo = new SoldierBasicInfoDO();
                    BeanUtils.copyProperties(base, baseInfo);
                    return decryptDo(baseInfo);
                }
            }
        } else {
            if (soldierApplyInfoDO.getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                SoldierBasicInfoDO base = soldierBasicInfoDao.getBySoldierId(soldierId);
                return decryptDo(base);
            }
        }
        return null;
    }

    private boolean checkTemporaryQrIsExpired(SoldierApplyInfoDO soldierApplyTemp) {
        int i = DateUtils.daysBetween(soldierApplyTemp.getCreateDatetime(), new Date());
        if (i > 7) {
            return true;
        }

        if ("DELETE".equals(soldierApplyTemp.getApplyStatus())) {
            return true;
        }
        return false;
    }

    @Override
    public SoldierBasicInfoDO getSoldierBasicInfoByCard(String idCardNo, String name) {
        return decryptDo(soldierBasicInfoDao.getSoldierBasicInfoByCardAndName(idCardNo, name));
    }

    @Override
    public List<SoldierBasicInfoDO> listSoldierBasicInfoByNameOfArea(String condition, String adminAreaCode) {
        return decryptDos(soldierBasicInfoDao.listSoldierBasicInfoByNameOfArea("%" + condition + "%", AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%"));
    }

    private List<SoldierBasicInfoDO> decryptDos(List<SoldierBasicInfoDO> soldierBasicInfoDOs) {
        if (CollectionUtil.isNotEmpty(soldierBasicInfoDOs)){
            soldierBasicInfoDOs.stream().forEach(l -> {
                decryptDo(l);
            });
        }
        return soldierBasicInfoDOs;
    }

    @Override
    public List<DataStatsCDO> countSubAreaPerson(String areaCode) {
        int nextCodeLength = AdminAreaCodeUtils.getNextCodeLength(areaCode);
        return soldierBasicInfoDao.countAreaPerson(areaCode + "%", nextCodeLength);
    }

    @Override
    public List<DataStatsCDO> queryAllSoldierResult(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.queryAllSoldierResult(areaLike, nextLevel, levelLength, null, null);
    }

    @Override
    public List<DataStatsCDO> queryAllSoldierResult(String adminAreaCode, Date beginDate, Date endDate) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.queryAllSoldierResult(areaLike, nextLevel, levelLength, beginDate, endDate);
    }

    
    @Override
    public List<SoldierBasicInfoDO> listByIdList(final List<String> idList) {
        return decryptDos(soldierBasicInfoDao.listByIdList(idList));
    }

    @Override
    public List<SoldierBasicInfoDO> listByIdCardNoList(List<String> idCardNoList) {
        return decryptDos(soldierBasicInfoDao.listByIdCardNoList(idCardNoList));
    }

    @Override
    public Boolean checkIfApplyQrSucc(String soldierId) {






        SoldierApplyInfoDO soldierApplyInfoDO = soldierApplyInfoService.getBySoldierId(soldierId);
        if (soldierApplyInfoDO == null) {
            
            List<SoldierApplyInfoDO> soldierApplyInfoDOS = soldierApplyInfoService.listBySoldierId(soldierId, SoldierApplyTypeEnum.OUTSIDER.name());
            if (!CollectionUtils.isEmpty(soldierApplyInfoDOS)) {
                if (soldierApplyInfoDOS.get(0).getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                    
                    return true;
                }
            }

            
            SoldierApplyInfoDO soldierApplyTemp = soldierApplyInfoService.getLastApplyOfSoldierId(soldierId, SoldierApplyTypeEnum.TEMPORARY.name());
            if (soldierApplyTemp != null) {
                if (!checkTemporaryQrIsExpired(soldierApplyTemp)) {
                    return true;
                }
            }
        } else {
            if (soldierApplyInfoDO.getStatus().equals(ProcessStatusEnum.SUCCESS.name())) {
                return true;
            }
        }
        return false;

    }

    @Override
    public List<DataStatsCDO> querySoldierQrSuccessResult(String areaLike, int nextLevel, int levelLength) {
        return soldierBasicInfoDao.querySoldierQrSuccessResult(areaLike, nextLevel, levelLength, null, null);
    }

    @Override
    public List<DataStatsCDO> querySoldierQrSuccessResult(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.querySoldierQrSuccessResult(areaLike, nextLevel, levelLength, null, null);
    }

    @Override
    public List<DataStatsCDO> querySoldierQrSuccessResult(String adminAreaCode, Date beginDate, Date endDate) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.querySoldierQrSuccessResult(areaLike, nextLevel, levelLength, beginDate, endDate);
    }

    @Override
    public int countQrSuccessByArea(String areaLike) {
        return soldierBasicInfoDao.countQrSuccessByArea(areaLike, null, null, null);
    }

    @Override
    public int countQrSuccessByArea(String areaLike, Date beginDate, Date endDate) {
        return soldierBasicInfoDao.countQrSuccessByArea(areaLike, null, beginDate, endDate);
    }

    @Override
    public Integer countSoldier(String areaLike) {
        return soldierBasicInfoDao.countSoldier(areaLike, null, null);
    }

    @Override
    public Integer countSoldier(String areaLike, Date beginDate, Date endDate) {
        return soldierBasicInfoDao.countSoldier(areaLike, beginDate, endDate);
    }

    @Override
    public List<SoldierBasicInfoDO> searchPreferentialTreatment(SoldierBasicInfoBSearcher soldierBasicInfoBSearcher) {
        return soldierBasicInfoDao.searchPreferentialTreatment(soldierBasicInfoBSearcher);
    }

    @Override
    public List<Map<String, String>> queryAreaQrDetail(String adminAreaCode) {
        if (adminAreaCode.equals("330100000000")) {
            return soldierBasicInfoDao.queryAreaQrDetail(adminAreaCode);
        }

        return soldierBasicInfoDao.queryAreaQrDetail(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<DataStatsCDO> queryAreaSoldierQrStats(String adminAreaCode, int isQr) {
        return soldierBasicInfoDao.queryAreaSoldierQrStats(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%", isQr, null, null);
    }

    @Override
    public List<DataStatsCDO> queryAreaSoldierQrStats(String adminAreaCode, int isQr, Date beginDate, Date endDate) {
        return soldierBasicInfoDao.queryAreaSoldierQrStats(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%", isQr, beginDate, endDate);
    }

    @Override
    public List<SoldierBasicInfoDO> searchAreaSoldierShortList(SoldierBasicInfoBSearcher searcher) {
        String adminAreaCode = searcher.getAdminAreaCode();
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        searcher.setAdminAreaCode(areaLike);

        return decryptDos(soldierBasicInfoDao.searchAreaSoldierShortList(searcher));
    }

    @Override
    public Integer countRetiredSoldier(String areaLike) {
        return soldierBasicInfoDao.countRetiredSoldier(areaLike);
    }

    @Override
    public Integer countSoldierByGender(String type, String adminAreaCode) {
        return soldierBasicInfoDao.countSoldierByGender(type, AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer setSoldierDead(String idCardNo, String info) {
        return soldierBasicInfoDao.setSoldierDead(idCardNo, info);
    }

    @Override
    public List<SoldierBasicInfoDO> listByNameAndAdminAreaCode(String name, String adminAreaCode) {
        return decryptDos(soldierBasicInfoDao.listByNameAndAdminAreaCode(name, AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%"));
    }

    @Override
    public List<SoldierBasicInfoDO> searchAreaDifficultySoldierShortList(SoldierBasicInfoBSearcher searcher) {
        String adminAreaCode = searcher.getAdminAreaCode();
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        searcher.setAdminAreaCode(areaLike);

        return decryptDos(soldierBasicInfoDao.searchAreaDifficultySoldierShortList(searcher));
    }

    @Override
    public List<SoldierBasicInfoDO> searchAreaUnemploymentSoldierShortList(SoldierBasicInfoBSearcher searcher) {
        String adminAreaCode = searcher.getAdminAreaCode();
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        searcher.setAdminAreaCode(areaLike);

        return decryptDos(soldierBasicInfoDao.searchAreaUnemploymentSoldierShortList(searcher));
    }

    @Override
    public Integer countRealDifficultyByArea(String adminAreaCode) {
        return soldierBasicInfoDao.countRealDifficultyByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer countRealUnemploymentByArea(String adminAreaCode) {
        return soldierBasicInfoDao.countRealUnemploymentByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<DataStatsCDO> countSubAreaRealUnemploymentDistri(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.countSubAreaRealUnemploymentDistri(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> countSubAgeRealUnemploymentDistri(String adminAreaCode) {
        return soldierBasicInfoDao.countSubAgeRealUnemploymentDistri(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<SoldierBasicInfoDO> searchNewNotInTpAll(SoldierBasicInfoBSearcher innerSearcher) {
        return decryptDos(soldierBasicInfoDao.searchNewNotInTpAll(innerSearcher));
    }

    @Override
    public List<DataStatsCDO> countSubAreaLowSide(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.countSubAreaLowSide(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> countSubAreaExtremelyPoor(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.countSubAreaExtremelyPoor(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> countSubAreaSubsistenceAllowance(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.countSubAreaSubsistenceAllowance(areaLike, nextLevel, levelLength);
    }

    @Override
    public Integer countSoldierType(String adminAreaCode, List<String> soldierTypes) {
        return soldierBasicInfoDao.countSoldierType(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%", soldierTypes);
    }

    @Override
    public Integer countPartyMember(String adminAreaCode) {
        return soldierBasicInfoDao.countPartyMember(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<SoldierBasicInfoDO> listAllSameAdminAreaCode(String adminAreaCode) {
        return decryptDos(soldierBasicInfoDao.listAllSameAdminAreaCode(adminAreaCode));
    }

    @Override
    public String getOriginAddress(String idCardNo) {
        return soldierBasicInfoDao.getOriginAddress(idCardNo);
    }

    @Override
    public String getAreaCodeByIdCardNo(String idCardNo) {
        return soldierBasicInfoDao.getAreaCodeByIdCardNo(idCardNo);
    }

    @Override
    public List<DataStatsCDO> listAgeRangeOfQrSuccess(String areaLike) {
        return soldierBasicInfoDao.listAgeRangeOfQrSuccess(areaLike);
    }

    @Override
    public Boolean isWestLakePreTreatment(String idCardNo) {
        int i = soldierBasicInfoDao.countWestLakePreTreatment(idCardNo);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<DataStatsCDO> groupBySoldierType(String adminAreaCode) {
        return soldierBasicInfoDao.groupBySoldierType(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<DataStatsCDO> groupByLabels(String adminAreaCode) {
        return soldierBasicInfoDao.groupByLabels(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer countSubsistenceAllowance(String adminAreaCode) {
        return soldierBasicInfoDao.countSubsistenceAllowance(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer countLowSide(String adminAreaCode) {
        return soldierBasicInfoDao.countLowSide(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer countExtremelyPoor(String adminAreaCode) {
        return soldierBasicInfoDao.countExtremelyPoor(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<DataStatsCDO> listAgeRange2(String adminAreaCode) {
        return soldierBasicInfoDao.listAgeRange2(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<SoldierBasicInfoDO> listProblemBirthday() {
        return decryptDos(soldierBasicInfoDao.listProblemBirthday());
    }

    @Override
    public Integer countPreTreatment() {
        return soldierBasicInfoDao.countPreTreatment();
    }

    @Override
    public Integer countPreTreatment(String adminAreaCode) {
        return soldierBasicInfoDao.countPreTreatmentByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public Boolean isKeyPreTreatment(String idCardNo) {
        int i = soldierBasicInfoDao.isKeyPreTreatment(idCardNo);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public SoldierBasicInfoDO getByMobile(String mobile) {
        return decryptDo(soldierBasicInfoDao.getByMobile(PrivacyEncUtils.encodeMobile(mobile)));
    }

    @Override
    public List<SoldierBasicInfoDO> listGetByBirthday(String month, String areaLike) {
        return decryptDos(soldierBasicInfoDao.listGetByBirthday(month, areaLike));
    }

    @Override
    public List<DataStatsCDO> listAgeRange3(String areaLike, String soldierType) {
        return soldierBasicInfoDao.listAgeRange3(areaLike, soldierType);
    }

    @Override
    public Integer countQrSuccessByAreaAndType(String areaLike, String applyType) {
        return soldierBasicInfoDao.countQrSuccessByArea(areaLike, applyType, null, null);
    }

    @Override
    public List<String> listExistedIdCardNo(Set<String> idCardNoSet) {
        return soldierBasicInfoDao.listExistedIdCardNo(Lists.newArrayList(idCardNoSet));
    }

    @Override
    public List<String> listAllIdCardNo() {
        return soldierBasicInfoDao.listAllIdCardNo();
    }

    @Override
    public SoldierBasicInfoDO getHzQrSucc(String soldierId) {
        SoldierBasicInfoDO bySoldierId = soldierBasicInfoDao.getBySoldierId(soldierId);
        BizCheckUtil.checkNotNull(bySoldierId, "还未获得杭州老兵码");
        return decryptDo(bySoldierId);
    }

    @Override
    public List<DataStatsCDO> groupPreferentialTreatmentAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.groupPreferentialTreatmentAreaMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public Integer countRetireByAreaAndYear(String adminAreaCode, String year) {
        return soldierBasicInfoDao.countRetireByAreaAndYear(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), year);
    }

    @Override
    public Integer countEntryByAreaAndYear(String adminAreaCode, String year) {
        return soldierBasicInfoDao.countEntryByAreaAndYear(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), year);
    }

    @Override
    public List<DataStatsCDO> groupByEducationLevelOfTp(String adminAreaCode, String tpType) {
        return soldierBasicInfoDao.groupByEducationLevelOfTp(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), tpType);
    }

    @Override
    public List<DataStatsCDO> groupAreaPersonOfTp(String adminAreaCode, String tpType) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.groupAreaPersonOfTp(areaLike, nextLevel, levelLength, tpType);
    }

    @Override
    public List<String> listAllMobile(String adminAreaCode) {
        return soldierBasicInfoDao.listAllMobile(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<SoldierBasicInfoDO> listBySoldierIdList(List<String> soldierIdList) {
        return decryptDos(soldierBasicInfoDao.listBySoldierIdList(soldierIdList));
    }

    @Override
    public List<SoldierBasicInfoDO> listTestIdTable(String areaCode) {
        return decryptDos(soldierBasicInfoDao.listTestIdTable(AdminAreaCodeUtils.getCutdAreaCode(areaCode)));
    }

    @Override
    public Integer countWorkAgeSoldier(String adminAreaCode) {
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        return soldierBasicInfoDao.countWorkAgeSoldier(areaLike);
    }

    @Override
    public List<DataStatsCDO> groupByPreferentialTreatmentType(String adminAreaCode) {
        return soldierBasicInfoDao.groupByPreferentialTreatmentType(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public Integer countPretreatmentOfSyfSoldier(String adminAreaCode) {
        return soldierBasicInfoDao.countPretreatmentOfSyfSoldier(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<DataStatsCDO> groupPretreatmentOfSyfSoldier(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return soldierBasicInfoDao.groupPretreatmentOfSyfSoldier(areaLike, nextLevel, levelLength);
    }

    @Override
    public Integer countByAreaAndPretreatLike(String adminAreaCode, String type) {
        return soldierBasicInfoDao.countByAreaAndPretreatLike(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), type);
    }

    @Override
    public List<DataStatsCDO> groupByPolitical(String adminAreaCode) {
        return soldierBasicInfoDao.groupByPolitical(adminAreaCode);
    }

    @Override
    public List<SoldierBasicInfoDO> searchElderlySoldier(SoldierBasicInfoBSearcher searcher) {
        return decryptDos(soldierBasicInfoDao.searchElderlySoldier(searcher));
    }

    @Override
    public List<DataStatsCDO> groupByPreferentialTreatmentTypeOfOrigin(String adminAreaCode) {
        return soldierBasicInfoDao.groupByPreferentialTreatmentTypeOfOrigin(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public SoldierBasicInfoDO getByIdCardNo_basicDb(String idCardNo) {
//        SoldierBasicInfoBSearch search = new SoldierBasicInfoBSearch();
//        search.setIdcard(PriInfoHandleUtil.encrypt(idCardNo));
//        List<SoldierBasicInfoB> ret = soldierBasicInfoBDao.search(search);
//        if (CollectionUtils.isEmpty(ret)) {
//            return null;
//        } else {
//            SoldierBasicInfoB res = ret.get(0);
//            SoldierBasicInfoDO soldierBasicInfoDO = new SoldierBasicInfoDO();
//            BeanUtils.copyProperties(res,soldierBasicInfoDO);
//            soldierBasicInfoDO.setGender(res.getGender().equals(1)?"MALE":"FEMALE");
//            soldierBasicInfoDO.setIdCardNo(PriInfoHandleUtil.decrypt(res.getIdcard()));
//            soldierBasicInfoDO.setRealName(res.getName());
//            soldierBasicInfoDO.setRootAdminAreaCode(res.getDomicileAreaCode());
//            soldierBasicInfoDO.setSoldierType(res.getSoldCateCode());
//            soldierBasicInfoDO.setResidentAdminAreaCode(res.getAdmDivCode());
//            try {
//                if (!StringUtils.isBlank(res.getEnlistDate())) {
//                    soldierBasicInfoDO.setEntryDatetime(DateUtil.parseDate(res.getEnlistDate()));
//                }
//                if (!StringUtils.isBlank(res.getRetiredDate())) {
//                    soldierBasicInfoDO.setRetireDatetime(DateUtil.parseDate(res.getRetiredDate()));
//                }
//            } catch (DateParseException e) {
//
//            }
//            return soldierBasicInfoDO;
//        }
        return getByIdCardNo(idCardNo);
    }

}

